import md5 from 'md5';
import request from '@/utils/util.request';
import type { Note, NoteList } from '@/types';

// 获取笔记列表
export function getNoteListApi(url: string, safePassword: string) {
	return request.get(`${url}/list`, {
		headers: {
			Authorization: `Bearer ${md5(safePassword)}`,
		},
	});
}

// 保存到云端
export function saveNoteToCloudApi(url: string, safePassword: string, note: Note) {
	return request.post(
		`${url}/add`,
		{ note },
		{ headers: { Authorization: `Bearer ${md5(safePassword)}` } }
	);
}

// 删除云端笔记
export function deleteNoteFromCloudApi(url: string, safePassword: string, noteId: string) {
	return request.delete(`${url}/delete?id=${noteId}`, {
		headers: { Authorization: `Bearer ${md5(safePassword)}` },
	});
}

// 同步本地笔记到云端
export function syncLocalNoteToCloudApi(url: string, safePassword: string, notes: NoteList) {
	return request.post(
		`${url}/sync`,
		{ notes },
		{ headers: { Authorization: `Bearer ${md5(safePassword)}` } }
	);
}
